/**
 * 
 * purpose: BUTTON GROUP...
 * @auth: Ning
 * date: 2009-8-20
 * 
 * */
package com.components.button
{
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	[Event(name="button_selected",type="com.components.button.ButtonEvents")]

	public class ButtonGroup extends EventDispatcher
	{
		//START...
		
		//PROPERTY...
		private var group:Array;
		
		private var _mouseEnabled:Boolean;
		
		private var _selectedButton:MCButton;
		
		
		//CONSTRUCTOR...
		public function ButtonGroup()
		{
			
			group = new Array;
			
		}
		
		//METHODS...
		
		//button click handler...
		private function buttonClickHandler(evt:MouseEvent):void
		{
			
			var button:MCButton = evt.currentTarget as MCButton;
						
			if(button == _selectedButton)
			{
				
				dispatchEvent(new ButtonEvents(ButtonEvents.BUTTON_SELECTED,button));
				
				return;
				
			}
						
			if(_selectedButton is MCButton)
			{
				
				_selectedButton.selected = false;
				
			}
			
			
			_selectedButton = button;
			
			dispatchEvent(new ButtonEvents(ButtonEvents.BUTTON_SELECTED,button));
			
		}
		
		//add button...
		public function addButton(button:MCButton):void
		{
			
			button.clickSelected = true;
			
			group.push(button);
			
			button.addEventListener(MouseEvent.CLICK,buttonClickHandler);
						
		}
		
		
		//add buttons...
		public function addButtons(...buttons):void
		{
		
			var buttonList:Array = buttons[0] is Array ? buttons[0] : buttons;
			
			var button:MCButton;
			
			for(var i:uint = 0;i < buttonList.length;i++)
			{
				
				button = buttonList[i] as MCButton;
												
				addButton(button);
				
			}
			
			
		}
		
		//select button...
		public function selectButton(button:MCButton):void
		{
			
			var index:uint;
			
			for(index = 0;index < group.length;index++)
			{
				
				if(button == group[index])
				{
					
					selectButtonByIndex(index);
					
					return;
					
				}
				
			}
			
		}
		
		//select button by index...
		public function selectButtonByIndex(index:uint):void
		{
			
			var button:MCButton = group[index] as MCButton;
			
			button.target.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
						
		}		
		
		//remove button by index...
		public function removeButtonByIndex(index:uint):void
		{
			
			var button:MCButton = group[index] as MCButton;
			
			if(	button == _selectedButton)
			{
				
				button.selected = false;
												
				_selectedButton = null;
				
			}		
			
			group.splice(index,1);
			
			button.clickSelected = false;
				
			button.removeEventListener(MouseEvent.CLICK,buttonClickHandler);
									
			
		}
		
		//get button count...
		public function get buttonCount():uint
		{
			
			return group.length;
			
		}
		
		//get selected index...
		public function get selectedIndex():int
		{
			
			var index:int = -1;
			
			if(selectedButton is MCButton)
			{
				
				for(var i:uint = 0;i < group.length;i++)
				{
					
					if(group[i] == selectedButton)
					{
						
						index = i;
						
						break;
						
					}
					
				}
				
			}		
			
			return index;	
			
		}
		
		//get selected button...
		public function get selectedButton():MCButton
		{
			
			return _selectedButton;
			
		}
		
		//get button by index...
		public function getButtonByIndex(index:uint):MCButton
		{
			
			return (group[index] as MCButton);
			
		}
		
		//remove all
		public function removeAll():void
		{
			
			var button:MCButton;
			
			for(var i:uint = 0;i < group.length;i++)
			{
				
				button = group[i] as MCButton;
				
				button.clickSelected = false;
				
				button.removeEventListener(MouseEvent.CLICK,buttonClickHandler);	
				
			}
			
			group = new Array;
			
			_selectedButton = null;
			
		}
		
		//mouse enabled...
		public function set mouseEnabled(val:Boolean):void
		{
			
			for(var i:uint = 0;i <group.length;i++)
			{
				
				if(i != selectedIndex)
				{
				
					group[i].target.mouseEnabled = val;
				
				}
				
			}
			
		}
		
		//get mouse enabled...
		public function get mouseEnabled():Boolean
		{
			
			return _mouseEnabled;
			
		}
		
		//END
	}
}